﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GTA;

namespace GTAHookAdv
{
    /// <summary>
    /// This class extends GTA.Blip class. with some nice functionalities
    /// </summary>
    public class g_Blip
    {

        private static Dictionary<BlipType, Blip[]> allBlips = new Dictionary<BlipType, Blip[]>();

        /// <summary>
        /// Gives all blips attached to a ped. This method takes time to run.
        /// </summary>
        /// <exception cref="InvalidOperationException">thrown if <see cref="Core.OnTick"/> is not assigned to your script <see cref="Script.Tick">Tick event</see>
        /// or if <see cref="Core.TickRefreshOption"/>does not include Blip value</exception>
        /// <seealso cref="Core.TickRefreshOption"/>
        public static Blip[] GetBlipsAttachedTo(Ped ped)
        {
            if (!Core.Ticked)
                throw new InvalidOperationException("Core.OnTick Must be assigned to an event first");
            if (!Core.IsTickRefreshOptionSetted(TickRefreshFlag.Blips))
                throw new InvalidOperationException("TickRefreshFlag.Blips MUST be applied to  Core.TickRefreshOption");
            
            List<Blip> result = new List<Blip>();
            if (ped == null)
                return null;
            foreach (Blip item in allBlips[BlipType.Ped])
            {
                if (item.Exists() &&  item.GetAttachedItem() != null && ((Ped)item.GetAttachedItem()) == ped)
                    result.Add(item);
            }
            return result.ToArray();
        }

        /// <summary>
        /// Guess :)
        /// </summary>
        public static void DestroyAllBlipsAttachedTo(Ped ped)
        {
            Blip[] found = GetBlipsAttachedTo(ped);
            if(found != null)
            {
                foreach (Blip item in found)
                {
                    try
                    {
                        if(item.Exists())
                            item.Delete();
                    }
                    catch
                    {
                    }
                }
            }
        }

        /// <summary>
        /// Guess :)
        /// </summary>
        public static Blip[] GetAllBlipsOfType(BlipType type)
        {
            if (!Core.Ticked)
                throw new InvalidOperationException("Core.OnTick Must be assigned to an event first");
            if (!Core.IsTickRefreshOptionSetted(TickRefreshFlag.Blips))
                throw new InvalidOperationException("TickRefreshFlag.Blips MUST be applied to  Core.TickRefreshOption");
            if (type != BlipType.Ped)
                throw new NotImplementedException("Only Ped type is supported at this time");
            return allBlips[type].ToArray();
        }

        internal static  void OnTick(object sender, EventArgs e)
        {
            allBlips.Clear();
            List<Blip> blips = new List<Blip>();
            Blip[] curBlips = GTA.Blip.GetAllBlipsOfType(BlipType.Ped);
            foreach (Blip item in curBlips)
            {
                if (item.GetAttachedItem() == null || item.GetAttachedItem() is Ped)
                    blips.Add(item);
            }
           curBlips = GTA.Blip.GetAllBlipsOfType(BlipType.Vehicle);
           foreach (Blip item in curBlips)
           {
               if (item.GetAttachedItem() is Ped)
                   blips.Add(item);
           }

           allBlips.Add(BlipType.Ped, blips.ToArray());
        }

      
    }
}
